﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using LView.CMS.IServices;
using LView.CMS.ViewModels;
using LView.CMS.Core;
using LView.CMS.Models;
using LView.CMS.Core.Extensions;
using System.Linq;

namespace LView.CMS.Services
{
    public class TestPageService : ITestPageService
    {
        public async Task<TableDataModel> LoadDataAsync(PageModel input)
        {
            var (recordList, recordCount) = (await MongodbHelper.Instance.FindListAsync<LExam>(
                sort: (x => x.Id),
                isDesc: true,
                options: null,
                pageIndex: input.Page,
                pageSize: input.Limit));

            return new TableDataModel
            {
                count = (int)recordCount,
                data = recordList,
            };
        }

        public async Task<BaseResult> AddOrModify(TestPageAddOrModify input)
        {
            var result = new BaseResult();
            var examEntity = new LExam();
            var exams = new List<ExamQuestion>();
            if (!input.Id.IsNullOrEmpty())
                await MongodbHelper.Instance.DeleteOneAsync<LExam>(x => x.Id == input.Id);
            try
            {
                input.ExamQuestions.ForEach(x =>
                {
                    var examOptions = new List<ExamOption>();
                    x.ExamOption.ForEach(p =>
                    {
                        examOptions.Add(new ExamOption
                        {
                            Id = Guid.NewGuid().ToString(),
                            Name = p.OptionName
                        });
                    });
                    var rightOptions = new List<string>();
                    x.RigthOptionIds.ForEach(y =>
                    {
                        examOptions.ForEach(x =>
                        {
                            var startIndex = x.Name.IndexOf('&') + 1;
                            if (x.Name.Substring(startIndex, x.Name.Length - startIndex) == y)
                            {
                                rightOptions.Add(x.Id);
                            }
                        });
                    });

                    examOptions.ForEach(x =>
                    {
                        x.Name = x.Name.Substring(0, x.Name.IndexOf('&'));
                    });

                    exams.Add(new ExamQuestion
                    {
                        Id = Guid.NewGuid().ToString(),
                        Name = x.QuestionName,
                        Score = x.QuestionScore,
                        Type = x.Type,
                        ExamOption = examOptions,
                        RigthOptionIds = rightOptions
                    });
                });
                examEntity.Id = Guid.NewGuid().ToString();
                examEntity.Name = input.TestPageName;
                examEntity.LimitMin = input.LimitMin;
                examEntity.LimitTimes = input.LimitTimes;
                examEntity.TotalScore = input.TotalScore;
                examEntity.PassScore = input.PassScore;
                examEntity.ExamQuestions = exams;
                await MongodbHelper.Instance.InsertOneAsync<LExam>(examEntity);
                result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
            }
            catch (Exception ex)
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg + $":{ex.Message}";
            }

            return result;
        }

        public async Task<BaseResult> DeleteIdsAsync(string[] Ids)
        {
            var result = new BaseResult();
            try
            {
                await MongodbHelper.Instance.DeleteManyAsync<LExam>(x => Ids.Contains(x.Id));
                result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
            }
            catch (Exception ex)
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg + $":{ex.Message}";
            }
            return result;
        }
    }
}
